package com.paireach.bw.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.paireach.bw.R;
import com.paireach.bw.activity.WarnInfoActivity;
import com.paireach.bw.activity.MainActivity;
import com.paireach.bw.adapter.ExceptionListAdapter;
import com.paireach.bw.base.BaseFragment;
import com.paireach.bw.bean.ExceptionInfoBean;
import com.paireach.bw.bean.ExceptionListBean;
import com.paireach.bw.event.AppEvent;
import com.paireach.bw.event.HttpErrorEvent;
import com.paireach.bw.event.HttpSuccessEvent;
import com.paireach.bw.http.RequestTag;
import com.paireach.bw.http.WayBillRequest;
import com.paireach.bw.utils.GsonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${cqc} on 2017/6/9.
 * 首页第3个tab：预警列表
 */

public class ExceptionListFrag extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {

    private static final String TAG = "ExceptionListFrag";
    private View view;
    private TextView layout_top_change;
    private ImageView layout_top_back;
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private List<ExceptionInfoBean> exceptionList = new ArrayList<>();
    private ExceptionListAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = LayoutInflater.from(context).inflate(R.layout.fragment_exception_list, container, false);
        findViews();
        initViews();

        Message msg = Message.obtain();
        msg.what = 100;
        handler.sendMessageDelayed(msg, 1000 * 60 * 10);
        return view;
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 100) {
                new WayBillRequest().getExceptionList(phoneNumber);
                Message message = Message.obtain();
                message.what = 100;
                handler.sendMessageDelayed(message, 1000 * 60 * 10);
            }
        }
    };

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        new WayBillRequest().getExceptionList(phoneNumber);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        handler.removeMessages(100);
    }

    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        dismissProgressDialog();
        swipeRefreshLayout.setRefreshing(false);
        if (event.getRequestTag() == RequestTag.EXCEPTION_LIST) {
            ExceptionListBean bean = GsonUtil.jsonToBean(event.getJson(), ExceptionListBean.class);
            if ("1".equals(bean.getStatus())) {
                exceptionList.clear();
                exceptionList.addAll(bean.getOrders());
                adapter.notifyDataSetChanged();

                if (exceptionList.size() > 0) {
                    ((MainActivity) getActivity()).tab3.setBadgeCount(exceptionList.size());
                } else {
                    ((MainActivity) getActivity()).tab3.removeBadge();
                }
            } else {
                exceptionList.clear();
                adapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
        swipeRefreshLayout.setRefreshing(false);
        exceptionList.clear();
        adapter.notifyDataSetChanged();
    }

    @Override
    public void applicationEvent(AppEvent event) {
        super.applicationEvent(event);
    }

    private void initViews() {
        layout_top_change.setText("预警列表");
        layout_top_back.setVisibility(View.GONE);

        swipeRefreshLayout.setOnRefreshListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        adapter = new ExceptionListAdapter(exceptionList);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new ExceptionListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(context, WarnInfoActivity.class);
                intent.putExtra("ExceptionInfoBean", exceptionList.get(position));
                startActivityForResult(intent, 101);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101) {
            if (resultCode == 102) {
                //当详情页提交后，就重新请求数据
                new WayBillRequest().getExceptionList(phoneNumber);
            }
        }
    }

    private void findViews() {
        layout_top_change = (TextView) view.findViewById(R.id.layout_top_change);
        layout_top_back = (ImageView) view.findViewById(R.id.layout_top_back);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    }

    @Override
    public void onRefresh() {
        new WayBillRequest().getExceptionList(phoneNumber);
    }
}
